package com.yjhb.android.sbz.fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.HelpCenterActivity;
import com.yjhb.android.sbz.activity.MsgActivity;
import com.yjhb.android.sbz.activity.SystemAnnouncementActivity;
import com.yjhb.android.sbz.activity.ViolationPublicityActivity;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.qcloud.tim.uikit.Observer;
import com.tencent.qcloud.tim.uikit.UnReadManager;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.tencent.qcloud.tim.uikit.component.action.PopDialogAdapter;
import com.tencent.qcloud.tim.uikit.component.action.PopMenuAction;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListAdapter;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListLayout;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationManagerKit;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationProvider;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/23 9:52
 */
public class IMFragment extends BaseFragment implements IUIKitCallBack, ConversationListLayout.OnItemClickListener,
        OnRefreshListener, ConversationListLayout.OnItemLongClickListener, Observer<Integer> {
    private final String TAG = "IMFragment";
    @BindView(R.id.msg_iv)
    ImageView msgIv;
    @BindView(R.id.msgView)
    LinearLayout msgView;
    @BindView(R.id.helpView)
    LinearLayout helpView;
    @BindView(R.id.violationsView)
    LinearLayout violationsView;
    @BindView(R.id.npcticeView)
    ConstraintLayout npcticeView;
    @BindView(R.id.conversationListLayout)
    ConversationListLayout conversationListLayout;
    @BindView(R.id.notMore_tv)
    TextView notMoreTv;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.sysUnread_tv)
    TextView sysUnread_tv;


    ConversationListAdapter adapter;

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_im;
    }

    @Override
    protected void initUI() {
        adapter = new ConversationListAdapter();
        conversationListLayout.setAdapter(adapter);

    }

    @Override
    protected void initEvent() {
        adapter.setOnItemClickListener(this);
        adapter.setOnItemLongClickListener(this);
        smartRefreshLayout.setOnRefreshListener(this);
        UnReadManager.getInstance().register(this);
    }


    @Override
    protected void initData() {
        ConversationManagerKit.getInstance().loadConversation(this);
    }

    @Override
    public void update(int type, Integer arg) {
        if (type == UnReadManager.TYPE_SYS) {
            if (arg > 0) {
                sysUnread_tv.setVisibility(View.VISIBLE);
                sysUnread_tv.setText("" + UnReadManager.getInstance().getUnreadSys());
            } else {
                sysUnread_tv.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onDestroy() {
        UnReadManager.getInstance().unregister(this);
        super.onDestroy();
    }

    @OnClick(R.id.msgView)
    void msgView() {
        ZxActivityTool.skipActivity(getContext(), MsgActivity.class);
    }

    @OnClick(R.id.helpView)
    void helpView() {
        ZxActivityTool.skipActivity(getContext(), HelpCenterActivity.class);
    }

    @OnClick(R.id.violationsView)
    void violationsView() {
        ZxActivityTool.skipActivity(getContext(), ViolationPublicityActivity.class);
    }

    @OnClick(R.id.npcticeView)
    void npcticeView() {
//        ZxActivityTool.skipActivity(getContext(), SystemNoticeActivity.class);
//        UnReadManager.getInstance().setUnreadSys(0);
//        sysUnread_tv.setVisibility(View.GONE);
        ZxActivityTool.skipActivity(getContext(), SystemAnnouncementActivity.class);
    }


    @Override
    public void onSuccess(Object data) {
        if (adapter != null)
            adapter.setDataProvider((ConversationProvider) data);
        if (smartRefreshLayout != null)
            smartRefreshLayout.finishRefresh();
    }

    @Override
    public void onError(String module, int errCode, String errMsg) {

    }


    @Override
    public void onItemClick(View view, int position, ConversationInfo messageInfo) {
        Tool.startChatActivity(getContext(), messageInfo.getId(), messageInfo.getTitle(), 0);
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        ConversationManagerKit.getInstance().loadConversation(this);
    }

    @Override
    public void OnItemLongClick(View view, int position, ConversationInfo messageInfo) {
        PopDialogAdapter adapter = null;
        QMUIListPopup popup = new QMUIListPopup(getContext(), QMUIPopup.DIRECTION_TOP, adapter = new PopDialogAdapter());
        popup.create(100 * ResManager.dp(), -2, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position2, long id) {
                if (position2 == 0) {
                    ConversationManagerKit.getInstance().deleteConversation(position, messageInfo);
                    popup.dismiss();
                }
            }
        });
        List<PopMenuAction> actions = new ArrayList<>();
        PopMenuAction action = new PopMenuAction();
        action.setActionName("刪除");
        actions.add(action);
        adapter.setDataSource(actions);
        popup.show(view);
    }


}
